package gui;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import logic.LogicHelper;
import logic.MainMidlet;

/**
 * starting game screen
 * @author Rekin
 *
 */
public class StartingScreen extends Canvas{
	private Sprite[] flyingCards;
	private Timer iTimer;
	private int[] movementDirections;
	private Font myUsedFontBig;
	private Font myUsedFontSmall;
	private Font myUsedFontMedium;
	
	/**
	 * default constructor
	 */
	public StartingScreen(){
		flyingCards=new Sprite[4];
		movementDirections=new int[8];
		myUsedFontBig=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
		myUsedFontMedium=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN | Font.STYLE_UNDERLINED, Font.SIZE_MEDIUM);
		myUsedFontSmall=Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN , Font.SIZE_SMALL);
		randomiseDirections();
		initFlyingCards();
		initTimer(50);
	}
	
	/**
	 * painting elements
	 */
	protected void paint(Graphics g) {
		g.setColor(0,0,0);
		g.fillRect(0, 0, getWidth(), getHeight());	 //black background
		g.setColor(240,210,10);
		g.setFont(myUsedFontBig);
		g.drawString("4Flush Mobile", getWidth()/2, myUsedFontBig.getBaselinePosition()+5, Graphics.BASELINE | Graphics.HCENTER);
		g.setFont(myUsedFontMedium);
		g.drawString("by Pawel Suszek", getWidth()/2, myUsedFontBig.getBaselinePosition()+5+
					myUsedFontMedium.getBaselinePosition()+5, Graphics.BASELINE | Graphics.HCENTER);
		g.setFont(myUsedFontSmall);
		g.drawString("Press any key or tap", getWidth()/2, myUsedFontBig.getBaselinePosition()+5+
				myUsedFontMedium.getBaselinePosition()+5+myUsedFontSmall.getHeight()+5, Graphics.BASELINE | Graphics.HCENTER);
		for(int i=0;i<flyingCards.length;++i){
			flyingCards[i].paint(g);
		}
	}

	/**
	 * randomises movement directions for cards
	 */
	private void randomiseDirections(){
		for(int i=0;i<movementDirections.length;++i){
			Random rand=new Random();
			int val;
			while((val=rand.nextInt()%4)==0){}
			try {
				Thread.sleep(25);
			} catch (InterruptedException e) {}
			
			movementDirections[i]=val;
		}
	}
	
	/**
	 * initiates flying cards
	 */
	private void initFlyingCards(){
    	for(int i=0;i<flyingCards.length;++i){
    		flyingCards[i]=new Sprite(LogicHelper.createImageFromResources("/flyingCards/card"+(i+1)+".png", this));
    		flyingCards[i].setPosition(randX(), randY());
    		flyingCards[i].setTransform(randRot());
    		try{
    		Thread.sleep(25);
    		}
    		catch(InterruptedException e){}
    	}
    }
    
	/**
	 * animates flying cards
	 */
    private void animateFlyingCards(){
    	handleOutOfBounds();
    	for(int i=0;i<flyingCards.length;++i){
    		flyingCards[i].setPosition(flyingCards[i].getX()+movementDirections[i], flyingCards[i].getY()+movementDirections[i+1]);
    	}
    }
    
    /**
     * handles cards' bounds hits
     */
    private void handleOutOfBounds(){
    	for(int i=0;i<flyingCards.length;++i){
    		if(flyingCards[i].getX()<1)
    			movementDirections[i]=Math.abs(movementDirections[i]);
    		else if(flyingCards[i].getX()+flyingCards[i].getWidth()>getWidth()-1)
    			movementDirections[i]=-Math.abs(movementDirections[i]);
    		
    		if(flyingCards[i].getY()<100)
    			movementDirections[i+1]=Math.abs(movementDirections[i]);
    		else if(flyingCards[i].getY()+flyingCards[i].getHeight()>getHeight()-1)
    			movementDirections[i+1]=-Math.abs(movementDirections[i]);
    	}
    }
    
    /**
     * randomise X
     * @return randomised value
     */
    private int randX(){
    	Random rand=new Random();
    	int val=getWidth()/2+rand.nextInt()%25;
    	return val;
    }
    
    /**
     * randomise Y
     * @return randomised value
     */
    private int randY(){
    	Random rand=new Random();
    	int val=getHeight()/2+rand.nextInt()%25;
    	return val;
    }
    
    /**
     * randomise rotation
     * @return randomised value
     */
    private int randRot(){
    	Random rand=new Random();
    	int rotVal= Math.abs(rand.nextInt())%4;
    	switch(rotVal){
    	case 0:
    		return Sprite.TRANS_NONE;
    	case 1:
    		return Sprite.TRANS_ROT90;
    	case 2:
    		return Sprite.TRANS_ROT180;
    	case 3:
    		return Sprite.TRANS_ROT270;
    	default:
    		return Sprite.TRANS_NONE;
    	}
    }
    
    /**
     * starts timer for cards movement updates
     * @param withIntervalMilis refresh rate
     */
    private void initTimer(int withIntervalMilis){
    	if(withIntervalMilis<=0){
    		return;
    	}
    	iTimer = new Timer();
		iTimer.schedule(new TimerTask(){
			public void run(){
				repaint();
				animateFlyingCards();
			} 
		} , 0, withIntervalMilis);
    }
    
    /**
     * pointer pressed event
     */
    protected void pointerPressed(int x, int y){
    	MainMidlet.getMidletInstance().showMainMenu();
	}
	
    /**
     * key pressed event
     */
    protected void keyPressed(int keyCode){
    	MainMidlet.getMidletInstance().showMainMenu();
    }
}
